//
//  UserTool.m
//  taodaji
//
//  Created by 谭亚明 on 2018/4/11.
//  Copyright © 2018年  All rights reserved.
//
#import "UserTool.h"
#import "UserModel.h"
#import "FamilyModel.h"
static UserModel *_user;
static FamilyModel *_family;

@implementation UserTool

+ (UserModel *)getLogInUser {
    
    @try {
        if (!_user) {
            
            // 获取用户信息
            // 获取归档路径
            NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"YPUser"];
            // 解档
            UserModel *user = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            _user = user;
        }
        return _user;
    } @catch (NSException *exception) {
        
    }
}

+ (FamilyModel *)getCurrentFamily{
    
    @try {
        if (!_family) {
            
            // 获取用户信息
            // 获取归档路径
            NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"YPUserfamily"];
            // 解档
            FamilyModel *user = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            _family = user;
        }
        return _family;
    } @catch (NSException *exception) {
        
    }
    
}

+ (void)clearUserInfocompletion:(void(^)( BOOL state))completion {
    @try {
        _user = nil;
        // 获取归档路径
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"YPUser"];
        // 删除这个归档文件
        if ([[NSFileManager defaultManager] removeItemAtPath:path error:nil]) {
            if (completion) {
                completion(YES);
            }
        }else{
            if (completion) {
                completion(NO);
            }
        }
    } @catch (NSException *exception) {
       
    }
}

+ (void)clearFamilycompletion:(void(^)( BOOL state))completion{
    
    @try {
        _family = nil;
        // 获取归档路径
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"YPUserfamily"];
        // 删除这个归档文件
        if ([[NSFileManager defaultManager] removeItemAtPath:path error:nil]) {
            if (completion) {
                completion(YES);
            }
        }else{
            if (completion) {
                completion(NO);
            }
        }
    } @catch (NSException *exception) {
       
    }
}

+(void)setUserInfo:(UserModel*)userInfo completion:(void(^)( BOOL state))completion{

    @try {
        [self clearUserInfocompletion:nil];
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"YPUser"];
        if([NSKeyedArchiver archiveRootObject:userInfo toFile:path]) {
            NSLog(@"归档成功");
            if (completion) {
               completion(YES);
            }
        } else {
            NSLog(@"归档失败");
            if (completion) {
               completion(NO);
            }
        }
    } @catch (NSException *exception) {
        
    }
}

+(void)setFamily:(FamilyModel*)family completion:(void(^)( BOOL state))completion{
    
    @try {
        [self clearFamilycompletion:nil];
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"YPUserfamily"];
        if([NSKeyedArchiver archiveRootObject:family toFile:path]) {
            NSLog(@"归档成功");
            if (completion) {
               completion(YES);
            }
        } else {
            NSLog(@"归档失败");
            if (completion) {
               completion(NO);
            }
        }
    } @catch (NSException *exception) {
        
    }
}

@end
